<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">📝</text>
						<text class="tool-title">考试占卜</text>
						<text class="tool-subtitle">考试运势和学业指导</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">考试信息</text>
						
						<view class="form-item">
							<text class="form-label">考试类型</text>
							<picker 
								mode="selector" 
								:range="examTypes" 
								:value="examTypeIndex"
								@change="onExamTypeChange"
								class="exam-type-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayExamType }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">考试科目</text>
							<input 
								v-model="params.subject" 
								placeholder="请输入考试科目"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">考试日期</text>
							<picker 
								mode="date" 
								:value="params.examDate" 
								@change="onExamDateChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayExamDate }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">准备情况</text>
							<view class="preparation-selector">
								<view 
									v-for="prep in preparationLevels" 
									:key="prep.value"
									class="prep-item"
									:class="{ active: params.preparation === prep.value }"
									@tap="selectPreparation(prep.value)"
								>
									<text class="prep-icon">{{ prep.icon }}</text>
									<text class="prep-text">{{ prep.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">担心问题（可选）</text>
							<textarea 
								v-model="params.concerns" 
								placeholder="请描述您担心的问题"
								class="concerns-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryExam"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="examResult">
					<view class="result-section">
						<text class="section-title">考试运势</text>
						
						<view class="exam-analysis">
							<view class="overall-exam">
								<view class="exam-score">
									<view class="score-circle">
										<text class="score-text">{{ examResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ examResult.overall.level }}</text>
								</view>
								<view class="exam-desc">
									<text class="desc-text">{{ examResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="exam-predictions" v-if="examResult.predictions">
								<view 
									v-for="(prediction, index) in examResult.predictions" 
									:key="index"
									class="prediction-item"
								>
									<view class="prediction-header">
										<text class="prediction-aspect">{{ prediction.aspect }}</text>
										<view class="prediction-level" :class="prediction.level.toLowerCase()">
											<text class="level-text">{{ prediction.level }}</text>
										</view>
									</view>
									<text class="prediction-description">{{ prediction.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 学习建议 -->
				<app-card v-if="examResult && examResult.studyTips">
					<view class="study-tips-section">
						<text class="section-title">学习建议</text>
						
						<view class="tips-list">
							<view 
								v-for="(tip, index) in examResult.studyTips" 
								:key="index"
								class="tip-item"
							>
								<view class="tip-icon">
									<text class="icon-text">📚</text>
								</view>
								<text class="tip-text">{{ tip }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 幸运元素 -->
				<app-card v-if="examResult && examResult.luckyElements">
					<view class="lucky-section">
						<text class="section-title">考试幸运元素</text>
						
						<view class="lucky-elements">
							<view class="element-item">
								<text class="element-label">幸运色</text>
								<text class="element-value">{{ examResult.luckyElements.color }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利方位</text>
								<text class="element-value">{{ examResult.luckyElements.direction }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">最佳时间</text>
								<text class="element-value">{{ examResult.luckyElements.time }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利物品</text>
								<text class="element-value">{{ examResult.luckyElements.item }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 注意事项 -->
				<app-card v-if="examResult && examResult.reminders">
					<view class="reminders-section">
						<text class="section-title">考试提醒</text>
						
						<view class="reminders-list">
							<view 
								v-for="(reminder, index) in examResult.reminders" 
								:key="index"
								class="reminder-item"
							>
								<view class="reminder-icon">
									<text class="icon-text">⚠️</text>
								</view>
								<text class="reminder-text">{{ reminder }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'ExamToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				examType: '',
				subject: '',
				examDate: '',
				preparation: '',
				concerns: ''
			},
			examResult: null,
			loading: false,
			toolsService: null,
			examTypeIndex: 0,
			examTypes: [
				'高考', '中考', '研究生入学考试', '公务员考试',
				'英语等级考试', '计算机等级考试', '职业资格考试', 
				'驾照考试', '专升本考试', '其他考试'
			],
			preparationLevels: [
				{ value: 'excellent', name: '充分准备', icon: '💯' },
				{ value: 'good', name: '准备较好', icon: '👍' },
				{ value: 'average', name: '一般准备', icon: '😐' },
				{ value: 'poor', name: '准备不足', icon: '😰' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.examType && this.params.subject.trim().length > 0 && 
				   this.params.examDate && this.params.preparation
		},
		
		displayExamType() {
			return this.params.examType || '选择考试类型'
		},
		
		displayExamDate() {
			return this.params.examDate || '选择考试日期'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
		
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				// 显示提示
				uni.showToast({
					title: '已为您登录，祝考试顺利！',
					icon: 'none',
					duration: 2000
				})
			} else {
				// 提示用户登录或完善信息
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onExamTypeChange(e) {
			this.examTypeIndex = e.detail.value
			this.params.examType = this.examTypes[e.detail.value]
		},
		
		onExamDateChange(e) {
			this.params.examDate = e.detail.value
		},
		
		selectPreparation(preparation) {
			this.params.preparation = preparation
		},
		
		async queryExam() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善考试信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getExamInfo(this.params)
				
				if (result.success) {
					this.examResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('考试占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.exam-type-picker,
		.date-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.preparation-selector {
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			gap: 16rpx;
			
			.prep-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #FF9500;
					background: linear-gradient(135deg, #FF9500 0%, #F57C00 100%);
					
					.prep-icon,
					.prep-text {
						color: white;
					}
				}
				
				.prep-icon {
					font-size: 32rpx;
					margin-bottom: 8rpx;
				}
				
				.prep-text {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.concerns-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.exam-analysis {
		.overall-exam {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #FF9500 0%, #F57C00 100%);
			border-radius: 16rpx;
			
			.exam-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.exam-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.exam-predictions {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.prediction-item {
				padding: 20rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.prediction-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 12rpx;
					
					.prediction-aspect {
						font-size: 28rpx;
						font-weight: 500;
						color: #000000;
					}
					
					.prediction-level {
						padding: 4rpx 12rpx;
						border-radius: 12rpx;
						
						&.excellent {
							background-color: #4CAF50;
						}
						
						&.good {
							background-color: #8BC34A;
						}
						
						&.average {
							background-color: #FF9800;
						}
						
						&.poor {
							background-color: #F44336;
						}
						
						.level-text {
							font-size: 20rpx;
							color: white;
							font-weight: 500;
						}
					}
				}
				
				.prediction-description {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.8);
					line-height: 1.5;
				}
			}
		}
	}
}

.study-tips-section,
.reminders-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.tips-list,
	.reminders-list {
		.tip-item,
		.reminder-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.tip-icon,
			.reminder-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.tip-text,
			.reminder-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}

.lucky-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.lucky-elements {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16rpx;
		
		.element-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.element-label {
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.element-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #FF9500;
			}
		}
	}
}
</style>